{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "def move_webm_files(source_dir, target_dir):\n",
    "    \"\"\"\n",
    "    移动所有.webm文件从源目录(包括子目录)到目标目录\n",
    "    \n",
    "    参数:\n",
    "        source_dir (str): 要搜索的源目录路径\n",
    "        target_dir (str): 要移动到的目标目录路径\n",
    "    \"\"\"\n",
    "    # 确保目标目录存在\n",
    "    if not os.path.exists(target_dir):\n",
    "        os.makedirs(target_dir)\n",
    "    \n",
    "    # 计数器\n",
    "    moved_files = 0\n",
    "    \n",
    "    # 遍历源目录及其所有子目录\n",
    "    for root, dirs, files in os.walk(source_dir):\n",
    "        for file in files:\n",
    "            if file.lower().endswith('.webm'):\n",
    "                # 获取文件完整路径\n",
    "                source_path = os.path.join(root, file)\n",
    "                \n",
    "                # 构造目标路径\n",
    "                # 为避免文件名冲突，可以添加原路径的部分信息\n",
    "                relative_path = os.path.relpath(root, source_dir)\n",
    "                if relative_path == '.':\n",
    "                    dest_path = os.path.join(target_dir, file)\n",
    "                else:\n",
    "                    # 替换路径分隔符为下划线以避免子目录创建\n",
    "                    safe_relative = relative_path.replace(os.sep, '_')\n",
    "                    dest_path = os.path.join(target_dir, f\"{safe_relative}_{file}\")\n",
    "                \n",
    "                try:\n",
    "                    # 移动文件\n",
    "                    shutil.move(source_path, dest_path)\n",
    "                    moved_files += 1\n",
    "                    print(f\"已移动: {source_path} -> {dest_path}\")\n",
    "                except Exception as e:\n",
    "                    print(f\"移动 {source_path} 失败: {e}\")\n",
    "    \n",
    "    print(f\"\\n完成! 共移动了 {moved_files} 个.webm文件\")\n",
    "\n",
    "source_directory = ''\n",
    "target_directory = ''\n",
    "\n",
    "move_webm_files(source_directory, target_directory)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 按照子文件夹名称移动文件"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import shutil\n",
    "\n",
    "def move_files_between_matching_subfolders(foder_dest, foder_src):\n",
    "    \"\"\"\n",
    "    移动两个文件夹中名称匹配的子文件夹中的所有文件到目标文件夹的对应子文件夹中。\n",
    "    \n",
    "    参数:\n",
    "        foder_dest: 待移动路径\n",
    "        foder_src: 目标路径\n",
    "    \"\"\"\n",
    "    # 获取子文件夹\n",
    "    subfolders_a = {name for name in os.listdir(foder_dest) \n",
    "                   if os.path.isdir(os.path.join(foder_dest, name))}\n",
    "    subfolders_b = {name for name in os.listdir(foder_src) \n",
    "                   if os.path.isdir(os.path.join(foder_src, name))}\n",
    "    \n",
    "    # 找出待移动和目标文件夹中都存在的子文件夹名\n",
    "    matching_subfolders = subfolders_a & subfolders_b\n",
    "    \n",
    "    if not matching_subfolders:\n",
    "        print(\"没有找到名称匹配的子文件夹\")\n",
    "        return\n",
    "    \n",
    "    for subfolder in matching_subfolders:\n",
    "        src_path = os.path.join(foder_src, subfolder)\n",
    "        dst_path = os.path.join(foder_dest, subfolder)\n",
    "        \n",
    "        # 移动子文件夹中的所有文件\n",
    "        for filename in os.listdir(src_path):\n",
    "            file_src = os.path.join(src_path, filename)\n",
    "            file_dst = os.path.join(dst_path, filename)\n",
    "            \n",
    "            if os.path.isfile(file_src):\n",
    "                shutil.move(file_src, file_dst)\n",
    "                print(f\"已移动: {file_src} -> {file_dst}\")\n",
    "            else:\n",
    "                print(f\"跳过非文件项: {file_src}\")\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    # 设置你的文件夹路径\n",
    "    foder_src = r\"\"  # 替换为你的待移动文件夹实际路径\n",
    "    foder_dest = r\"\"  # 替换为你的目标文件夹实际路径\n",
    "    \n",
    "    # 检查路径是否存在\n",
    "    if not all(os.path.isdir(f) for f in [foder_dest, foder_src]):\n",
    "        print(\"错误: 指定的文件夹路径不存在\")\n",
    "    else:\n",
    "        move_files_between_matching_subfolders(foder_dest, foder_src)\n",
    "        print(\"文件移动完成\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
